<template>
  <div class="edit-box">
    <el-input class="title" v-model="editArticle.title" placeholder="标题"></el-input>
    <div class="tag">
      <el-tag
        :key="tag"
        v-for="tag in editArticle.tags"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="tagInputVisible"
        v-model="tagInputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" :class="[editArticle.tags.length === 0 ? 'none-tag' : '']" size="small" @click="showInput">新增标签</el-button>
      <el-button class="save" type="primary" size="small" @click="save">保存</el-button>
    </div>
     <div id="editor" class="editor"></div>
    <span class="modifiy-time" v-if="updated_at">最后修改时间:{{updated_at | moment('YYYY-MM-DD HH:mm:ss')}}</span>
  </div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
import E from "wangeditor";
import * as API from "./../../store/api";
export default {
  name: "Acticle",
  data() {
    return {
      acticleId: undefined,
      editArticle: {
        title: "",
        author: "",
        tags: [],
        content: ""
      },
      updated_at: "",
      tagInputVisible: false,
      tagInputValue: "",
      editor: null
    };
  },
  computed: {
    ...mapGetters(["userInfo"])
  },
  methods: {
    ...mapActions([
      "ACTICLE_ACTION",
      "ACTICLE_CREATE_ACTION",
      "ACTICLE_UPDATE_ACTION",
      "IMG_UPLOAD_ACTION"
    ]),
    async meImgAdd(pos, file) {
      let result = await this.IMG_UPLOAD_ACTION({ type: "image", file });
      this.$refs.me.$img2Url(pos, result.data.url);
    },
    async save() {
      if (!this.editArticle.title) {
        console.log("请输入标题！");
      }
      if (!this.editArticle.content) {
        console.log("请输入内容！");
      }
      this.editArticle.content = this.editor.txt.html();
      this.editArticle.author = this.userInfo._id;
      let params = JSON.parse(JSON.stringify(this.editArticle));
      let result;
      if (this.acticleId) {
        params["_id"] = this.acticleId;
        result = await this.ACTICLE_UPDATE_ACTION(params);
      } else {
        result = await this.ACTICLE_CREATE_ACTION(params);
      }
      if (result.code === 1) {
        this.$router.push("/index/acticles");
      }
    },

    handleClose(tag) {
      this.editArticle.tags.splice(this.editArticle.tags.indexOf(tag), 1);
    },

    showInput() {
      this.tagInputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.tagInputValue;
      if (inputValue) {
        this.editArticle.tags.push(inputValue);
      }
      this.tagInputVisible = false;
      this.tagInputValue = "";
    }
  },
  created() {
    this.acticleId = this.$route.params.id;
  },
  async mounted() {
    const token = sessionStorage.getItem("vf");
    this.editor = new E("#editor");
    this.editor.customConfig.uploadImgServer = API.Upload;
    this.editor.customConfig.uploadImgParams = {
      name: "image"
    };
    this.editor.customConfig.uploadImgHeaders = {
      vf: token
    };
    this.editor.create();

    if (this.acticleId !== "add") {
      let acticle = await this.ACTICLE_ACTION(this.acticleId);
      this.updated_at = acticle.updated_at;
      this.editArticle = {
        title: acticle.title,
        content: acticle.content,
        tags: acticle.tags
      };
      this.editor.txt.html(acticle.content);
    } else {
      this.acticleId = null;
    }
  }
};
</script>
<style lang="scss" scoped>
.edit-box {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  height: 100%;
  img {
    max-width: 100% !important;
  }
  .title {
    margin-bottom: 1rem;
  }
  .modifiy-time {
    margin-top: 0.5rem;
    font-size: 14px;
    color: #aaa;
  }
  .editor {
    box-sizing: border-box;
    margin: 0 auto;
    flex: 1;
    width: 100%;
    // max-width: 780px;
  }
  .tag {
    margin-bottom: 1rem;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .none-tag {
    margin-left: 0px;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
